//
//  CZOauthVC.m
//  XinLang_OC
//
//  Created by WONG on 16/2/21.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZOauthVC.h"
#import "CZNetworkTool.h"
#import "CZUserAccount.h"
#import "AppDelegate.h"

@interface CZOauthVC ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;



@end

@implementation CZOauthVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view = self.webView;
    self.webView.delegate = self;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[[CZNetworkTool sharedHttpTool] oauthURL]];
    [self.webView loadRequest:request];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"填充" style:UIBarButtonItemStylePlain target:self action:@selector(autoFill)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cloce)];
}


#pragma mark - selector

- (void)autoFill {
    NSString *js = @"document.getElementById('userId').value='13527261476'; document.getElementById('passwd').value='QWERDFb8161';";
    // 让webView执行js代码
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

- (void)cloce {
    [SVProgressHUD dismiss];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIWebViewDelegate

// webView开始加载连接
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [SVProgressHUD showWithStatus:@"正在加载..." maskType:SVProgressHUDMaskTypeBlack];
}

// webView加载完成某个链接
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
}

// webView加载链接,每当webView加载一个链接都会通过这个代理方法来询问我们这个地址是否可以加载 true表示可以加载, false 就不加载.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *urlStr = [request URL].absoluteString;
    
    if ([urlStr hasPrefix:redirect_uri]) {
        NSRange range = [urlStr rangeOfString:@"code="];
        NSString* code = [urlStr substringFromIndex:range.location + range.length];
        
        [[CZNetworkTool sharedHttpTool] loadAccessToken:code finished:^(id reposponseObject, NSError *error) {
            // 出错
            if (reposponseObject == nil || error != nil) {
                NSLog(@"error = %@",error);
                [SVProgressHUD showErrorWithStatus:@"您的网络不给力..." maskType:SVProgressHUDMaskTypeBlack];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self cloce];
                });
                
                return;
            }
            NSLog(@"reposponseObject = %@",reposponseObject);
            // 没有出错
            CZUserAccount *account = [CZUserAccount userWithDict:reposponseObject];
            
            [account saveAccout];
            [self cloce];
            
            [AppDelegate switchRootViewController:NO];
        }];
        
        return NO;
    }
    
    
    return YES;
}

#pragma mark - lazy load

- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
    }
    return _webView;
}

@end





















